import Mock from "mockjs";
import * as mUtils from '@/util/mUtils'
const BASE_URL = process.env.VUE_APP_URL
const userList = [
  {
    userId: '1001',
    userName: '张三',
    userNo: '194123456',
    userRole: 'admin',
    password: '123456'
  },
  {
    userId: '1002',
    userName: '李四',
    userNo: '196123456',
    userRole: 'user',
    password: '123456'
  },
]

// 检查登陆
const checkLogin = function(req) {
  let resData = null;
  const { userId, password } = JSON.parse(req.body)
    resData = {
      success: false,
      data: {},
      err: {
        code: 1,
        msg: "验证失败",
      }
    }
    for (let i = 0; i < userList.length; i++) {
      if (userId == userList[i].userId && password == userList[i]. password) {
      resData = {
        success: true,
        data: {
          userName: userList[i].userName,
          userNo: userList[i].userNo,
          userId: userList[i].userId,
          userRole: userList[i].userRole
        },
        err: {
          code: 0,
          msg: "验证成功",
        }
      }
      break;
    }
  }
  return resData
};

// 获取当前用户信息
const getUserInfo = function(req) {
  let resData = null;
  const {userId} = mUtils.param2Obj(req.url)
  resData = {
    success: false,
    data: {
    },
    err: {
      code: 1,
      msg: '获取信息失败'
    }
  }
  for(let i=0; i < userList.length; i ++){
    if(userId == userList[i].userId){
      resData = {
        success:true,
        data:{
          userName: userList[i].userName,
          userNo: userList[i].userNo,
          userId: userList[i].userId,
          userRole: userList[i].userRole
        },
          err: {
            code: 0,
            msg: '获取用户信息成功'
        }
    }
    break;
  }
  }
  return resData;
};

// 获取所有用户列表
const getAllUserInfo = function(req) {
  let resData = null;
  let allUserList = [];
  for (let i = 0; i < userList.length; i++) {
    allUserList.push({
      userName: userList[i].userName,
      userNo: userList[i].userNo,
      userId: userList[i].userId,
      userRole: userList[i].userRole,
    })
  }
  resData = {
    success: true,
    data: {
      allUserList
    },
    err: {
      code: 0,
      msg: "获取用户列表成功"
    }
  }
  return resData
};

// 编辑用户
const editUser = function(req) {
  let resData = null;
  const {
    userId,
    userName,
    userNo,
    userRole
  } = JSON.parse(req.body)
  resData = {
    success: false,
    data: {
    },
    err: {
      code: 1,
      msg: '信息不完整，修改失败'
    }
  }
  if(userId){
    for(let i=0; i < userList.length; i ++){
      if(userList[i].userId == userId){
        userList[i].userNo == userNo;
        userList[i].userName == userName;
        userList[i].userRole == userRole;
        resData = {
          success:true,
          data:{
          },
            err: {
              code: 0,
              msg: "修改成功"
        }
      }
    }
  }
}
  return resData;
}

// 添加用户
const addUser = function(req) {
  let resData = null;
  let addObj = {};
  const {
    userName,
    userNo,
    userRole
  } = JSON.parse(req.body)
  if(userName){
  //以时间作为唯一ID
  addObj.userId = Number(Date.now());
  addObj.userNo =  userNo;
  addObj.userName = userName;
  addObj.userRole =  userRole;
  userList.push(addObj);
  resData = {
    success: true,
    data: {},
    err: {
      code: 0,
      msg: "添加成功"
    }
  }
}else{
      resData = {
        success:false,
        data:{
        },
          err: {
            code: 1,
            msg: '添加用户失败'
        }
      }
    }

  return resData;
}

Mock.mock(`${BASE_URL}/user/login`, 'post', checkLogin)
Mock.mock(RegExp(`${BASE_URL}/user/userInfo` + '.*'), 'get', getUserInfo)
Mock.mock(`${BASE_URL}/user/userInfo`, 'post', addUser)
Mock.mock(`${BASE_URL}/user/userInfo`, 'put', editUser)

Mock.mock(`${BASE_URL}/user/users`, 'get', getAllUserInfo)